﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Driver;
using Newtonsoft.Json.Serialization;
using SimpleInjector;
using Sustainalytics.Entities.ESGRatings;
using Sustainalytics.ESGRatings.DataContract.ESG;
using Sustainalytics.ESGRatings.DataLayer;
using Sustainalytics.MongoDB.Extensions;
using Sustainalytics.Synchronization.Interface;
using Sustainalytics.Utils.Enumerable;

namespace Sustainalytics.ESGRatings.Computing.ProfileTest
{
    class Program
    {
        readonly static SyncContext _syncContext = new SyncContext()
        {
            GAConnectionStrings = new Dictionary<string, string>
            {
                { "ClientDW", "mongodb://localhost:27017/ClientDW_A" }

            },
            Report = new ConcurrentBag<string>()
        };

        protected static Container SimpleInjectorContainer;

        private const string ServiceUrl = "http://localhost:10280/esgcomputingserv/api/v1";
        private static readonly string _uri = string.Format("{0}{1}", ServiceUrl, "/esgcompute");
        //private static readonly string _uri = string.Format("{0}{1}", ServiceUrl, "/health/clean");
        

        private static readonly JsonMediaTypeFormatter JsonMediaTypeFormatter = new JsonMediaTypeFormatter
        {
            SerializerSettings = { ContractResolver = new CamelCasePropertyNamesContractResolver() }
        };

        static void Main(string[] args)
        {
            TestComputeRatings();
            //PopulateSet();
        }

        static void TestComputeRatings()
        {
            //SimpleInjectorContainer = SimpleInjectorConfig.Register(new SyncContextLogger(_syncContext), _syncContext.GAConnectionStrings);

            //var computationEngine = SimpleInjectorContainer.GetInstance<ComputationEngine>();


            //computationEngine.Compute();

            //var logging = computationEngine.Log;

            var client = new HttpClient();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));



            //var res = client.PostAsync(_uri, string.Empty, JsonMediaTypeFormatter).Result;

                var companyId = new Guid("6c6845ff-4d9e-4f4d-b920-962af834ef0a");

            var options = new ESGRatingsOptionsDto
            {
                MatrixId = new Guid("00000000-0000-0000-0000-000000000000"), // "Default Weight Matrix"
                //MatrixId = new Guid("4f013fd0-740c-412b-9843-5980e8c73e64"), // Stoox

                RefUnivId = new Guid("71f2972e-3153-4f83-ac6e-d82bd676481f"), // Default
                //RefUnivId = new Guid("8f00443c-d944-44c5-910d-2afa6c1eca04"), // STOXX Global 1800

                CompaniesIds = new List<Guid>() { companyId }, //Chevron
                //CompaniesIds = new List<Guid>()
                //{
                //    new Guid("dac0eb43-299d-4e2a-99c0-77e23df7eb48"), // "Dechra Pharmaceuticals PLC"
                //    new Guid("aaa077ee-d037-4a75-8ea6-4a6bd5336b26"), // "OSG Corporation"
                //    new Guid("0962cd9e-0984-4177-a437-77338d25d26b"), // "Nippon Shokubai Co., Ltd."
                //},

                GroupBy = ComparativeGroupEnum.PeerGroup,
                CompanyType = CompanyTypeEnum.Public,// CompanyTypeEnum.Public & CompanyTypeEnum.Private
                TemplateType = TemplateTypeEnum.Combined,// TemplateTypeEnum.TypeA & TemplateTypeEnum.TypeB

                IncludeAggregatedScores = true,
                IncludeRatings = true,
                IncludeWeights = true,
                IncludeTopPerformersInfo = false,
                IncludeIndWeightedScores = true,
                IncludeAggregatedWeights = true,
                IncludeCompaniesInGroup = true
            };

            var esgRatingServiceUrl = $"http://localhost:10280/esgratingserv/api/v1/company/{companyId}/esgCompanyReport";

            var parameters = new EsgCompanyReportParameters()
            {
                CompanyId = companyId,
                MatrixId = options.MatrixId,
                ReferenceUniverseId = options.RefUnivId,
                HasComprehensiveResearch = true,
                IsSummaryReport = false,
                EsgProfileId = Guid.Empty,
                ComparativeGroup = (int)ComparativeGroupEnum.PeerGroup,
                CompanyType = (int)CompanyTypeEnum.Combined,
                TemplateType = (int)TemplateTypeEnum.Combined,
                FileName = string.Empty
                
            };

            var result = client.PostAsync(esgRatingServiceUrl, parameters, JsonMediaTypeFormatter).Result;

            //var result = client.PostAsync(_uri, options, JsonMediaTypeFormatter);

            var esgRatingsDto = result.Content.ReadAsAsync<ESGRatingsDto>(JsonMediaTypeFormatter.Yield());
        }

        static void PopulateSet()
        {
            var localConnectionString = "mongodb://localhost:27017/RPHistoricalDataSet";
            var remoteConnectionString = "mongodb://54.154.132.43:27017/RPHistoricalDataSet";

            var localCollection = MongoFactory.GetCollectionFromDatabase<HistoricalDataSet>(localConnectionString);
            var remoteCollection = MongoFactory.GetCollectionFromDatabase<HistoricalDataSet>(remoteConnectionString);


            var set = remoteCollection
                .Find(dataSet => true)
                .Sort(new SortDefinitionBuilder<HistoricalDataSet>().Descending(x => x.CreationDate))
                .FirstOrDefault();

            localCollection.InsertOne(set);

        }
    }
}
